package bhbt.controller.admin.member;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bhbt.controller.core.BaseController;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;






@Controller
 
public class MemberAddController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private ParamWageService paramWageService;
	@Resource
	private DailiService dailiService;
	
	@RequestMapping("admin/member/memberAddShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//获取list
		Map formMap = getParameterMap(request);
		List dailiList = dailiService.queryForList("select", formMap);
		request.setAttribute("dailiList",dailiList);
	 
		//接点图传过来的参数
		String fatherLoginName = MapUtil.getString(formMap, "fatherLoginName");
		String qu = MapUtil.getString(formMap, "qu");
		
		request.setAttribute("qu",qu);
		request.setAttribute("fatherLoginName",fatherLoginName);
		
		return "admin/member/memberAdd";
	}

	@RequestMapping("admin/member/memberAdd.shtml")
    public String add(ModelMap model,HttpServletRequest request, HttpServletResponse response) throws Exception{
		Map formMap = getParameterMap(request);
		/*****************验证******************/
		String loginName = MapUtil.getString(formMap, "loginName");
		boolean result = true;
		//账号
		if(memberService.executMemberByLoginName(loginName)!=null){
			setMessage(model,"该会员账号已经存在。");
			result = false;
		 
			 
		}
		
		String regEx = "[\u4e00-\u9fa5]"; 
		Pattern pat = Pattern.compile(regEx); 
		Matcher matcher = pat.matcher(loginName); 
		if (matcher.find()){ 
			setMessage(model,"登录名不能包含中文。");
			result = false;
			 
		}
		 
		//过滤登录名的特殊字符
		if(loginName.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length()!=0){
		 
			setMessage(model,"该登录名含有特殊字符，请重新填写！");
		} 
		
		if(loginName.length()>loginName.replaceAll(" ", "").length()){
			setMessage(model,"该登录名含有空格，请重新填写！");
		}
		 
		//推荐人
		String recommendLoginName = MapUtil.getString(formMap, "recommendLoginName");
		 
		Integer recommendId = null;
		Member recommend = null;
		if(!recommendLoginName.equals("")&&recommendLoginName!=null){
			recommend = memberService.executMemberByLoginName(recommendLoginName);
			if(recommend != null){
				
				if(recommend.getAccountActive().equals("F")){
					setMessage(model,"推荐人未激活。");
					result=false;
				}else{
					recommendId = recommend.getId();
				} 
			}else{
				setMessage(model,"推荐人不存在。");
				result = false;
				 
				 
			}
		}
		 
		
		 
		  
		
		if(!result){
			List dailiList = dailiService.queryForList("select", formMap);
			request.setAttribute("dailiList",dailiList);
			return "admin/member/memberAdd";
		}
		String onePsw= MapUtil.getString(formMap,"onePassword");
		String onePassword = CryptUtil.getInstance().encrypt2String(onePsw,CryptUtil.keyStr);
		
		String twoPsw= MapUtil.getString(formMap,"twoPassword");
		String twoPassword = CryptUtil.getInstance().encrypt2String(twoPsw,CryptUtil.keyStr);
		Member member = new Member(); 
		 
		member.setLoginName(loginName);
		member.setOnePassword(onePassword);
		member.setTwoPassword(twoPassword);
		member.setRecommendId(recommendId);
		 
		ParamWage paramWage1003= this.paramWageService.getByKey(1003);
		 
		
		member.setDailiId(10);
		member.setPayNum(paramWage1003.getValue());	
		member.setCardNo(MapUtil.getString(formMap,"cardNo"));
		member.setRemark(MapUtil.getString(formMap,"remark"));
		member.setWei(MapUtil.getString(formMap,"wei"));
		member.setQq(MapUtil.getString(formMap,"qq"));
		member.setTel(MapUtil.getString(formMap,"tel"));
		member.setPersonName(MapUtil.getString(formMap,"personName"));
		
//		member.setBankShi(MapUtil.getString(formMap, "bankShi"));
//		member.setBankName(MapUtil.getString(formMap, "bankName"));
//		member.setBankAddress(MapUtil.getString(formMap, "bankAddress"));
//		member.setBankCardNo(MapUtil.getString(formMap, "bankCardNo"));
//		member.setBankSheng(MapUtil.getString(formMap, "bankSheng"));
//		member.setBankPersonName(MapUtil.getString(formMap, "bankPersonName"));
//		 
		
		memberService.saveOne(member);
		
		return "redirect:memberNoActiveList.shtml";
		
    } 
	
	@RequestMapping("admin/member/ajaxGetRecommendByName.shtml")
	@ResponseBody
	public void ajaxTuRecommendAction( HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println("*********ajaxGetRecommendByName.shtml***********");
		Object retObj = null;
		Map formMap = getParameterMap(request); 
		String recommendLoginName= MapUtil.getString(formMap,"recommendLoginName");
		Member member=memberService.executMemberByLoginName(recommendLoginName);
		String personName="";
		HashMap resultformMap = new HashMap();
		if(member!=null){
			
			retObj = member;
			personName=member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);
			
			 
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
	}
	
	 
	@RequestMapping("admin/member/ajaxGetFatherByName.shtml")
	@ResponseBody
	public void ajaxTuFahterAction( HttpServletRequest request, HttpServletResponse response) {
		
		System.out.println("****1011111111******:");
		Object retObj = null;
		Map formMap = getParameterMap(request); 
		String fatherLoginName= MapUtil.getString(formMap,"fatherLoginName");
		System.out.println("**********:"+fatherLoginName);
		Member member=memberService.executMemberByLoginName(fatherLoginName);
		String personName="";
		HashMap resultformMap = new HashMap();
		if(member!=null){
			retObj = member;
			personName=member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
	}
	
	
	@RequestMapping("admin/member/ajaxGetCenterByName.shtml")
	@ResponseBody
	public void ajaxTuCenterAction( HttpServletRequest request, HttpServletResponse response) {
		
		
		Object retObj = null;
		Map formMap = getParameterMap(request); 
		String centerLoginName= MapUtil.getString(formMap,"centerLoginName");
		Member member=memberService.executMemberByLoginName(centerLoginName);
		String personName="";
		HashMap resultformMap = new HashMap();
		if(member!=null&&member.getAccountCenter().equals("T")){
			
			retObj = member;
			personName=member.getPersonName();
			resultformMap.put("member", member);
			resultformMap.put("personName", personName);
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
	}
	
	/**
     * loginName检测  ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("admin/member/ajaxGetMemberByName.shtml")
	@ResponseBody
	public void ajaxGetMemberByName( HttpServletRequest request, HttpServletResponse response) {
		
		
		Map formMap = getParameterMap(request); 
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);
		 
		HashMap resultformMap = new HashMap();
		 
		if(member==null){
			
			renderSuccess(response, true, resultformMap);
		}else{
			
			
			renderSuccess(response, false, resultformMap); 
			
		}
		
	}
	
	@RequestMapping("admin/member/ajaxGetMemberListByName.shtml")
	@ResponseBody
	public void ajaxGetMemberListByName( HttpServletRequest request, HttpServletResponse response) {
		
		
		Map formMap = getParameterMap(request); 
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);
		 
		Map<String, Object> memberMap = new HashMap<String, Object>();
		 
		List<Member> list = this.memberService.queryForList("select", memberMap);
		 
		if(member==null){
			
			renderSuccess(response, true, list);
		}else{
			
			
			renderSuccess(response, false, list); 
			
		}
		
	}

}
